package com.xiaobai.myblog.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RequiresAuthentication
@ServerEndpoint("/chat/{userId}")
@Component
@Slf4j // 注解在类上；为类提供一个 属性名为log 的 log4j 日志对像
public class WebSocketService {

    public static Map<String, Session> sessionMap = new ConcurrentHashMap<String, Session>();

    @OnOpen
    public void onOpen(@PathParam("userId") String userId, Session session) {
        log.info("WebSocketService onOpen: "+userId);
        if(sessionMap == null) {
            sessionMap = new ConcurrentHashMap<String, Session>();
        }
        sessionMap.put(userId, session);
    }

    @OnClose
    public void OnClose(@PathParam("userId") String userId) {
        log.info("WebSocketService OnClose: "+userId);
        sessionMap.remove(userId);
    }
    @OnMessage
    public void OnMessage(@PathParam("userId") String userId,Session session,String message) {
        log.info("WebSocketService OnMessage: "+message);
        for(Session session_ : sessionMap.values()) {
            session_.getAsyncRemote().sendText(message);
        }
    }

    @OnError
    public void error(Session session, Throwable t) {

        t.printStackTrace();
    }
}